package main.java.com.rmisorter.sorters;
import java.rmi.RemoteException;

@SuppressWarnings({"rawtypes","unchecked"})
public class InsertionSort extends SuperSort {
	private static final long serialVersionUID = 1L;

	public InsertionSort() throws RemoteException {
		super();
	}

	public Comparable[] sort(Comparable[] listToSort) {
		validateEmptyList(listToSort);
		validateAlreadySortedList(listToSort);
		
		insertionSort(listToSort, 0, listToSort.length);
		return listToSort;
	}
	
	public void insertionSort(Comparable[] listToSort, int start, int end) {   	
    	int i, j;
    
        for (i = start + 1; i < end; i++) {
            Comparable val = listToSort[i];
            j = i - 1;
            
            while (j >= 0 && val.compareTo(listToSort[j]) < 0) {
            	listToSort[j + 1] = listToSort[j];
                j--;
            }
            listToSort[j + 1] = val;
        }
	}
	
}
